import sys
import time

import pytest
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from DButils import *
from common1.login_operation import *
from common1.loan_operation import *
from common1.loan_readdata import get_data


class TestLoan:

    @pytest.fixture(scope="function", autouse=True)
    def setup_teardown(self):
        service = Service("chromedriver.exe")
        self.driver = webdriver.Chrome(service=service)
        self.driver.get("http://localhost:90")
        self.driver.maximize_window()
        self.driver.implicitly_wait(5)
        yield
        self.driver.quit()

    #正常申请贷款测试
    @pytest.mark.parametrize("username, password, money, day, expect", get_data(0))
    def test_normal(self, username, password, money, day, expect):
        login_normal(self.driver, username, password)
        loan(self.driver, money, day)

        sql = "select * from loan where userID  = %s and amount = %s and applyStatus = %s and term = %s"
        param = [1, float(money), 0, day]
        expect = len(select(sql, param))
        assert expect == 1

        sql = "delete from loan where userID = %s and amount = %s and applyStatus = %s and term = %s"
        param = [1, float(money), 0, day]
        update(sql, param)

    #申请贷款-失败
    @pytest.mark.parametrize("username, password, money, day, expect", get_data(1))
    def test_fail(self, username, password, money, day, expect):
        login_normal(self.driver, username, password)
        loan(self.driver, money, day)

        result = self.driver.find_element(By.XPATH, "/html/body/div[2]/div[2]/div/div/div/div/div/div/div/div[3]/div/div").text
        assert result == expect





